package com.itheima.sc.service.sms;

import cn.hutool.core.convert.Convert;
import com.itheima.sc.api.sms.SmsFlashApi;
import com.itheima.sc.config.ComException;
import com.itheima.sc.pojo.sms.FlashPromotion;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SmsFlashService {


    @DubboReference
    private SmsFlashApi smsFlashApi;

    //分页查询秒杀活动
    public PageResult getAll(String keyword, String page, String pageSize) {
        //keyword 进行关键字模糊查询 page,pageSize进行分页查询
        return smsFlashApi.list(keyword,Convert.toInt(page),Convert.toInt(pageSize));
    }
    //添加秒杀活动
    public void addFlash(FlashPromotion flashPromotion) {
        flashPromotion.setCreateTime(new Date(System.currentTimeMillis()));
        smsFlashApi.addFlash(flashPromotion);
    }
    //删除秒杀活动
    public void deleteFlash(Long id) {
        //首先判断状态
       int status= smsFlashApi.findStatus(id);
        if (status==1){
            throw new ComException("活动上线中,无法删除,请先修改状态");
        }
        smsFlashApi.delete(id);
    }
    //更新秒杀活动状态
    public void updateStatusFlash(Long id,int stats) {
      FlashPromotion flashPromotion=new FlashPromotion();
      flashPromotion.setId(id);
      flashPromotion.setStatus(stats);
      smsFlashApi.updateFlash(flashPromotion);
    }
    //编辑活动
    public void updateFlash(FlashPromotion flashPromotion) {
     smsFlashApi.updateFlash(flashPromotion);
    }

    //查询活动详情
    public FlashPromotion getFlashInfo(Long id) {
      return smsFlashApi.selectFlash(id);
    }
}
